import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_app/pages/home/info/info_item.dart';
import 'package:flutter_app/utils/dio_http.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// @description: 资讯
class Informationpage extends StatefulWidget {
  const Informationpage({Key? key}) : super(key: key);

  @override
  _InformationpageState createState() => _InformationpageState();
}

List _list = [
  {
    "imagsrc": 'images/banner.png',
    "title": "111111111111111111111",
    "from": '网易新闻',
    "time": '2021',
    "pushUrl": '/login'
  },
  {
    "imagsrc": 'images/banner.png',
    "title": "2222",
    "from": '网易新闻',
    "time": '2022',
    "pushUrl": '/login'
  },
  {
    "imagsrc": 'images/banner.png',
    "title": "2222",
    "from": '网易新闻',
    "time": '2022',
    "pushUrl": '/login'
  },
  {
    "imagsrc": 'images/banner.png',
    "title": "2222",
    "from": '网易新闻',
    "time": '2022',
    "pushUrl": '/login'
  },
  {
    "imagsrc": 'images/banner.png',
    "title":
        "This site hosts Flutter's API documentation. Other documentation can be found at the following locations:",
    "from": '网易新闻',
    "time": '2022',
    "pushUrl": '/login'
  },
];

class _InformationpageState extends State<Informationpage> {
  late List _infoList = [];
  _getInfoLidst() async {
    var res = await DioHttp.of(context).get('/home/news');
    var resString = json.decode(res.toString());
    var status = resString['status'];
    String desc = resString["description"] ?? "内部错误";
    if (status.toString().startsWith('2')) {
      var infoList = resString["body"];
      setState(() {
        _infoList = infoList;
      });
    } else {
      EasyLoading.showError(desc);
    }
  }

  @override
  void initState() {
    _getInfoLidst();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('资讯'),
        ),
        body: Padding(
          padding: const EdgeInsets.only(left: 0),
          child: ListView(
            children: _infoList.map((e) => InfoItem(data: e)).toList(),
          ),
        ));
  }
}
